package org.example.course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.CourseChapters;
import org.example.commonapi.domain.CourseSonChaptersVideo;
import org.example.commonapi.domain.R;
import org.example.course.service.CourseChaptersService;
import org.example.course.service.CourseSonChaptersVideoService;
import org.example.course.until.CourseUntil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 大章节控制层
 */
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseChaptersController {

    @Resource
    private CourseChaptersService courseChaptersService;

    @Resource
    private CourseSonChaptersVideoService courseSonChaptersVideoService;


    /**
     * 获取课程章节
     * @param courseId
     * @return
     */
    @GetMapping("/list/chapters/{courseId}")
    public R listChapters(@PathVariable Long courseId){
        LambdaQueryWrapper<CourseChapters> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseChapters::getCourseId,courseId);
        List<CourseChapters> list=courseChaptersService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加课程章节
     * @param courseChapters
     * @return
     */
    @PostMapping("/insert/chapters")
    public R insertChapters(@RequestBody CourseChapters courseChapters){
        R result= CourseUntil.isCourseChapters(courseChapters);
        if(result!=null)return result;
        courseChaptersService.save(courseChapters);
        return R.success();
    }

    /**
     * 修改课程章节
     * @param courseChapters
     * @return
     */
    @PutMapping("/update/chapters")
    public R updateChapters(@RequestBody CourseChapters courseChapters){
        R result= CourseUntil.isCourseChapters(courseChapters);
        if(result!=null)return result;
        courseChaptersService.updateById(courseChapters);
        return R.success();
    }


    /**
     * 删除课程章节
     * @param id
     * @return
     */
    @DeleteMapping("/delete/chapters/{id}")
    public R deleteChapters(@PathVariable Long id) {
        courseChaptersService.removeById(id);
//        底下的小章节也删除
        LambdaQueryWrapper<CourseSonChaptersVideo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseSonChaptersVideo::getChapterId,id);
        courseSonChaptersVideoService.remove(queryWrapper);
        return R.success();
    }

}
